<?php
namespace app\api\service\provider;
/**
 * UserProvider
 * @Author Vee7
 * @Date 2021/12/23
 * @Time 15:01
 * @Description Vee7
 */

use app\api\common\Auth;
use app\api\service\serv\UserService;
use think\facade\Cache;
use app\api\model\UserModel;


class UserProvider implements UserService
{
    public function checkLogin($username,$password)
    {
        $model = new UserModel();
        $query = $model->where('username',$username)->column(['id','username','password','email','mobile','status','type_id','avatar_src'])
        ['0'];
        if(empty($query)){
            return $return = [
                'data' => null,
                'msg'   => '账号不存在',
                'code'  => 10002
            ];
        }else{
            if($query['status'] == 0){
                return $return = [
                    'data' => null,
                    'msg'   => '账号被封禁',
                    'code'  => 10010
                ];
            }
            if(password_verify($password,$query['password'])){
                $header = request()->header();
                $time = time();
                $this->userInfo = $query;
                $sign = Auth::getSignkey(
                    [
                        'auid' => $query['id'],
                        'did' => $header['did'], //需要前端自行添加header头参数did 设备号 如：12345678
                        'time' => $time,
                        'version' =>$header['version'], //需要前端自行添加header头参数version 版本号 如：1.0
                        'ip' => request()->ip()
                    ]
                );
//                $data = [
//                    'sign' => $sign,
//                    'username' => $this->username,
//                    'uid'  => $this->uid,
//                    'time' => $time
//                ];
                $token = Auth::getToken($time,request()->ip());
                $data = [
                    'token' => $token,
                    'username' => $query['username'],
                    'uid'  => $query['id'],
                    'time' => $time
                ];

                Cache::set($token,$sign,3600*24*30);
                Cache::set($time.'apiUserInfo'.request()->ip(),$this->userInfo,3600*24*30);
                return $return = [
                    'data' => [
                        'token' => $token,
                        'time' => $time,
                        'userInfo' => $this->userInfo
                    ],
                    'msg'   => '登陆成功',
                    'code'  => 200
                ];
            }else{
                return $return = [
                    'data' => null,
                    'msg'   => '密码错误',
                    'code'  => 10002
                ];
            }
        }
    }

}